package gamecomponents.menu;

import gamecomponents.graphic.Blitable;
import input.InputManager;

import java.awt.Point;

import com.threed.jpct.Texture;

/**
 * A Menu Button
 * @author Team Deer
 * 
 */
public class MenuButton implements Blitable {
	
	private final Point position;
	private final Texture inactiveTexture;
	private final Texture activeTexture;
	private final int width;
	private final int height;
	private final int alpha;
	private boolean isActive;
	private boolean hasChanged;

	// Constructor
	
	/**
	 * Constructs a new Menu Button
	 * @param x
	 *            - the x-coordinate of the button
	 * @param y
	 *            - the y-coordinate of the button
	 * @param activeTexture
	 *            - The texture representing an active button
	 * @param inactiveTexture
	 *            - The texture representing an inactive button
	 * @param width
	 *            - The width of the button
	 * @param height
	 *            - The height of the button
	 * @param alpha
	 *            - The alpha value
	 */
	public MenuButton(final Texture activeTexture,
			final Texture inactiveTexture, final int x, final int y,
			final int width, final int height, final int alpha) {
		this.activeTexture = activeTexture;
		this.inactiveTexture = inactiveTexture;
		this.position = new Point(x, y);
		this.width = width;
		this.height = height;
		this.alpha = alpha;
		this.isActive = false;
		this.hasChanged = false;
	}

	// Methods

	/**
	 * @param isActive
	 *            the isActive to set
	 */
	public void setActive(final boolean isActive) {
		this.isActive = isActive;
	}
	
	/**
	 * @return the Texture
	 */
	@Override
	public Texture getTexture() {
		return this.isActive ? this.activeTexture : this.inactiveTexture;
	}

	/**
	 * @return the position
	 */
	@Override
	public Point getPosition() {
		return this.position;
	}

	/**
	 * @return the width
	 */
	@Override
	public int getSrcWidth() {
		return this.inactiveTexture.getWidth();
	}

	/**
	 * @return the height
	 */
	@Override
	public int getSrcHeight() {
		return this.inactiveTexture.getHeight();
	}

	/**
	 * @return the width
	 */
	@Override
	public int getWidth() {
		return this.width;
	}

	/**
	 * @return the height
	 */
	@Override
	public int getHeight() {
		return this.height;
	}

	@Override
	public int getAlpha() {
		return this.alpha;
	}

	/**
	 * 
	 * @param p
	 *            - The point to check.
	 * @return true if p is over the buttons area
	 */
	public boolean pointIsOver(final Point p) {

		if (p.x >= this.position.x && p.x <= (this.position.x + this.width)) {
			if (p.y > this.position.y && p.y < (this.position.y + this.height)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 
	 * @return true if button has changed.
	 */
	public boolean hasChanged() {
		return this.hasChanged;
	}

	/**
	 * Updates this button.
	 * @param inputManager the inputmanager
	 */
	public void update(final InputManager inputManager) {
		this.hasChanged = false;
		if (this.pointIsOver(inputManager.getMousePosition())) {
			if (!this.isActive) {
				this.isActive = true;
				this.hasChanged = true;
			}
		} else {
			if (this.isActive) {
				this.isActive = false;
				this.hasChanged = true;
			}
		}
	}

}
